home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 1.iso / dist / fw_exmh.idb / usr / freeware / lib / exmh-2.5 / PgpDecryptExpect.z / PgpDecryptExpect
Text File  |  2002-07-08  |  5KB  |  184 lines

  1. # -*-Mode: tcl;-*-
  2. #
  3. # To be interpreted by expectk
  4. #
  5. # This is run by exmh, currenly only when decrypting certain PGP messages.
  6. # See Preferences->Pgp->Help or the source code for more information.
  7. #
  8. # Arguments: v, infile, outfile, exmh-bg name
  9.  
  10. wm withdraw .
  11.  
  12. if {[llength $argv] < 4} {
  13.     puts "PgpDecryptExpect should be invoked by exmh"
  14.     exit 1
  15. }
  16.  
  17. set v [lindex $argv 0]
  18. set infile [lindex $argv 1]
  19. set outfile [lindex $argv 2]
  20. set exmhbg [lindex [lrange $argv 3 [expr [llength $argv] - 1]] 0]
  21. set badpass 0
  22. set encrypted 0
  23.  
  24. # GetPassword tries (if necessary) to get the the pass phrase using
  25. # Exmh's Misc_GetPass command.  It sets pgpPass if it gets a
  26. # legitamite password.
  27.  
  28. proc GetPassword { key } {
  29.     global spawn_id expect_out badpass v
  30.  
  31.     if {[string match conventional $key]} {
  32.     set password [Exmh [list Pgp_Misc_GetPass $v \
  33.                         "Enter [Exmh [list set pgp($v,fullName)]] Password" \
  34.             "symmetric encryption - enter pass phrase"] ]
  35.     } else {
  36.     Match_PrivateExact $key keyid keydesc
  37.     
  38.     if $badpass {
  39.         Exmh [list unset pgp($v,pass,$keyid)]
  40.     }
  41.     
  42.     if {$keyid!={} && [Exmh [list info exists pgp($v,pass,$keyid)]]} {
  43.         set password [Exmh [list set pgp($v,pass,$keyid)]]
  44.     } else {
  45.         set password [Exmh [list Pgp_Misc_GetPass $v \
  46.                         "Enter [Exmh [list set pgp($v,fullName)]] Password" \
  47.                         "password for $keydesc"] ]
  48.             Exmh [list set pgp($v,pass,$keyid) $password]
  49.     }
  50.     set badpass 1
  51.     }
  52.  
  53.     exp_send "${password}\r"
  54.     InterpretResponse
  55. }
  56.  
  57. # search privatekeys for the keyid exactly matching keydesc
  58. # returns keyid or {}
  59. proc Match_PrivateExact { key keyidVar keydescVar } {
  60.     global v
  61.     upvar $keyidVar keyid
  62.     upvar $keydescVar keydesc
  63.  
  64.     foreach elem [Exmh [list set pgp($v,privatekeys)]] {
  65.         # One key keys (main only)
  66.         set hexid [lindex $elem 0]
  67.         if {[regexp $key $hexid]} {
  68.             Debug "<Match_PrivateExact> $hexid"
  69.             set keyid $hexid
  70.             set keydesc [lindex $elem 4]
  71.             return
  72.         }
  73.         # Double key keys (main and sub)
  74.         set hexid [lindex $elem 2]
  75.         if {[regexp $key $hexid]} {
  76.             Debug "<Match_PrivateExact> $hexid"
  77.             set keyid $hexid
  78.             set keydesc [lindex $elem 4]
  79.             return
  80.         }
  81.     }
  82.     set keyid {}
  83.     set keydesc $key
  84. }
  85.  
  86. # Saves typing - this calls the given Exmh command.
  87. proc Exmh {cmd} {
  88.     global exmhbg
  89.     send $exmhbg $cmd
  90. }
  91.  
  92. proc Debug {text} {
  93. #    send exmh [concat Exmh_Debug [list $text]]
  94.     if {![catch {open /home/markus/***exmh***/dlog {APPEND WRONLY}} fd]} {
  95.         puts $fd $text
  96.         close $fd
  97.     }
  98. }
  99.  
  100. proc InterpretResponse { } {
  101.     global spawn_id timeout expect_out badpass encrypted v
  102.  
  103.     expect {
  104.     [Exmh [list set pgp($v,expectpat,passprompt)]] {
  105.             if {[info exists expect_out(1,string)]} {
  106.             GetPassword $expect_out(1,string)
  107.             } else {
  108.                 # There was no keyid and the program is prompting for a password?
  109.                 # It must be conventional encryption then. I hope this hasn't
  110.                 # unforeseen effects
  111.                 set encrypted 1
  112.                 GetPassword conventional
  113.             }
  114.     }
  115.     -re [Exmh [list set pgp($v,expectpat,conventional)]] {
  116.         set encrypted 1
  117.         set expect_out(1,string) conventional
  118.         InterpretResponse
  119.     }
  120.         -re [Exmh [list set pgp($v,expectpat,publickey)]] {
  121.             # I hope 40 is enough on slow computers
  122.             set timeout 40
  123.             set badpass 0
  124.             set encrypted 1
  125.             InterpretResponse
  126.         }
  127.     -re [Exmh [list set pgp($v,expectpat,secretmissing)]] {
  128.         set encrypted 1
  129.         catch {regsub -all \r $expect_out(1,string)$expect_out(2,string) {} msg}
  130.         Done $msg 1
  131.     }
  132.     -re [Exmh [list set pgp($v,expectpat,nopgpfile)]] {
  133.         Done $expect_out(1,string) 2
  134.     }
  135.     eof {
  136.             Debug Eof
  137.         regsub -all \r $expect_out(buffer) {} msg
  138.         Done [string trim $msg]
  139.     }
  140.     }
  141.     Debug Error
  142.     Done "Unknown Error" 2
  143. }
  144.  
  145. # Done is the procedure that calls the Exmh continuation command
  146. # before this process exits.
  147. proc Done {msg {error 0} } {
  148.     global outfile infile encrypted
  149.  
  150.     if !$encrypted {
  151.     set msg "Note: File may not have been encrypted.\n\n$msg"
  152.     }
  153.  
  154.     if {$error==2} {
  155.     set msg "Error processing with PGP\n$msg"
  156.     }
  157.  
  158.     if {[string trim $msg]==""} {
  159.     set msg "Message encrypted with PGP"
  160.     }
  161.     Exmh [list set pgpmsg $msg]
  162.     puts $msg
  163.     exit $error
  164. }
  165.  
  166. expect_after timeout {
  167.     Done "Unknown Error" 2
  168. }
  169. set timeout 4
  170.  
  171. # Now execute the whole thing
  172. eval [concat spawn -nottycopy -noecho [Exmh [list set pgp($v,cmd_DecryptExpect)]]]
  173.  
  174. # Don't ask me why, but when I comment out the following line my
  175. # program ceases to work under some conditions.  Mail me
  176. # <bescoto@usa.net> if you figure it out.
  177. expect -re . {}
  178.  
  179. catch {InterpretResponse} error
  180. Debug $error
  181.  
  182. exit 3
  183.